<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 14:47
 */

namespace app\controllers;

use app\lib\BaseFunction;
use app\models\Baoxiao;
use app\models\Staff;
use yii;

class ReimburseController extends ApiBaseController
{
    public $model;

    public function init()
    {
        parent::init();
        $this->model = new Baoxiao();
    }

    public function actionList()
    {
        $page                = yii::$app->request->get('page', 1);
        $staffModel          = new Staff();
        $pageSize            = yii::$app->request->get('pageSize', 15);
        $where               = [];
        $where['status']     = yii::$app->request->get('status');
        $where['type']       = yii::$app->request->get('type');
        $where['type2']      = yii::$app->request->get('type2');
        $where['invoice']    = trim(yii::$app->request->get('invoice'));
        $where['begin_date'] = yii::$app->request->get('begin_time');
        $where['end_date']   = yii::$app->request->get('end_time');
        if ($this->user['role'] == 10) {
            $where['job_number'] = $staffModel->getStaffByGroup(4);
        }
        $where['job_number'] = yii::$app->request->get('staff');
        $where['status']     = yii::$app->request->get('status', 0);
        $list                = $this->model->all($where, ($page - 1) * $pageSize, $pageSize, ['create_at' => SORT_DESC, 'id' => SORT_DESC]);
        foreach ($list as $k => $v) {
            $list[$k]['detail'] = json_decode($v['detail'], true);
        }
        $size = $this->model->get_baoxiao_size($where);
        return json_encode(['list' => $list, 'size' => $size]);
    }

    public function actionCommon()
    {
        $model       = new Staff();
        $staff       = $model->get_all_saleman();
        $property    = yii::$app->params['reimburse_type'];
        $propertyTmp = [];
        foreach ($property as $k => $v) {
            $obj           = [];
            $obj['id']     = $k;
            $obj['value']  = $v;
            $propertyTmp[] = $obj;
        }
        $type    = yii::$app->params['reimburse_type2'];
        $typeTmp = [];
        foreach ($type as $k => $v) {
            $obj          = [];
            $obj['id']    = $k;
            $obj['value'] = $v;
            $typeTmp[]    = $obj;
        }
        return json_encode(['staff' => $staff, 'property' => $propertyTmp, 'type' => $typeTmp]);
    }

    /**
     * 个人报销信息
     */
    public function actionMy()
    {
        $page                = yii::$app->request->get('page', 1);
        $pageSize            = yii::$app->request->get('pageSize', 15);
        $where               = [];
        $where['status']     = yii::$app->request->get('status', 0);
        $where['job_number'] = $this->user['job_number'];
        $list                = $this->model->all($where, ($page - 1) * $pageSize, $pageSize, ['create_at' => SORT_DESC]);
        foreach ($list as $k => $v) {
            $list[$k]['detail'] = json_decode($v['detail'], true);
        }
        $size = $this->model->get_baoxiao_size($where);
        return json_encode(['list' => $list, 'size' => $size]);
    }

    /**报销删除
     * @return string
     */
    public function actionDel()
    {
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => 'not allowed']);
        }

        $id = yii::$app->request->post('id');
        if (!is_numeric($id) || $id <= 0) {
            return \json_encode(['code' => 0, "msg" => '参数错误']);
        }
        $model = new Baoxiao();
        $re    = $model->one(['id' => $id]);

        if (!$re) {
            return \json_encode(['code' => 0, 'msg' => '无此记录']);
        }
        if ($re['status'] == 2) {
            return \json_encode(['code' => 0, 'msg' => '报销已通过,无法删除']);
        }

        $re = $model->delete(['id' => $id]);

        if ($re) {
            return \json_encode(['code' => 1, 'msg' => '删除成功']);
        } else {
            return \json_encode(['code' => 0, 'msg' => '删除失败,请重试']);
        }
    }

    /**报销审核
     * @return string
     */
    public function actionVerify()
    {
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => 'not allowed']);
        }

        $id = yii::$app->request->post('id');
        if (!is_numeric($id) || $id <= 0) {
            return \json_encode(['code' => 0, "msg" => '参数错误']);
        }
        $model = new Baoxiao();
        $re    = $model->one(['id' => $id]);

        if (!$re) {
            return \json_encode(['code' => 0, 'msg' => '无此记录']);
        }
        if ($re['status'] == 2) {
            return \json_encode(['code' => 0, 'msg' => '报销已通过']);
        }

        $re = $model->save(['status' => 2, 'time' => time()], ['id' => $id]);

        if ($re) {
            return \json_encode(['code' => 1, 'msg' => '审核成功']);
        } else {
            return \json_encode(['code' => 0, 'msg' => '删除失败,请重试']);
        }
    }

    public function actionPay()
    {
        $id = yii::$app->request->post('id');
        if (!is_numeric($id) || $id <= 0) {
            return \json_encode(['code' => 0, 'msg' => '参数错误']);
        }
        if (!yii::$app->request->isPost) {
            return \json_encode(['code' => 0, 'msg' => 'not allowed']);
        }

        BaseFunction::check_edit_auth($this->user['role']);
        $model = new Baoxiao();
        $info  = $model->one(['id' => $id]);
        if ($info['status'] != 2) {
            return json_encode(['code' => 0, 'msg' => '审核未通过，无法打款']);
        }
        if ($info['pay_staff']) {
            return \json_encode(['code' => 0, 'msg' => '已经打款']);
        }
        $data['pay_staff'] = $this->user['job_number'];
        $data['pay_time']  = time();

        if ($model->save($data, ['id' => $id])) {
            echo json_encode(['code' => 1, 'msg' => '更新成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '更新失败，请重试']);
        }

    }
}
